package net.jxtz.jxtzos.util;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

public class StampToDateAndDateToStamp {

    private static final StampToDateAndDateToStamp DATE_AND_DATE_TO_STAMP = new StampToDateAndDateToStamp();
    private StampToDateAndDateToStamp(){}
    public static StampToDateAndDateToStamp getInstance() {
        return DATE_AND_DATE_TO_STAMP;
    }

    /*
     * 将时间转换为时间戳
     */
    @Deprecated
    public static String dateToStamp(String time, String formates) {
        SimpleDateFormat sdf = new SimpleDateFormat(formates);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String stamp = "";
        if (!"".equals(time)) {//时间不为空
            try {
                // 取至秒
//                stamp = String.valueOf(sdf.parse(time).getTime()/1000);

                stamp = String.valueOf(sdf.parse(time).getTime() / 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {    //时间为空
            long current_time = System.currentTimeMillis();  //获取当前时间
            stamp = String.valueOf(current_time); // 毫秒级
        }
        return stamp;
    }


    /*
     * 将时间戳转换为时间
     */
    @Deprecated
    public static String stampToDate(int time, String formats) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat(formats);
        return sdf.format(new Date(time * 1000L));

    }

    /**
     * 验证时间是否过期（月）
     * @param dateTimeFormatterToIsoLocalDate 需要比较的时间，日期格式：'yyyy-MM-dd'
     * @param minusMonth 超过的月数
     * @return true 过期，false 未过期
     */
    public static boolean isExpireOnMonth(String dateTimeFormatterToIsoLocalDate, long minusMonth) {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.minusMonths(minusMonth);
        LocalDate parse = LocalDate.parse(dateTimeFormatterToIsoLocalDate, DateTimeFormatter.ISO_LOCAL_DATE);
        return parse.isBefore(localDate);
    }


    /**
     * 验证时间是否过期（日）
     * @param dateTimeFormatterToIsoLocalDate 需要比较的时间，日期格式：'yyyy-MM-dd'
     * @param minusDay 超过的天数
     * @return true 过期，false 未过期
     */
    public static boolean isExpireOnDay(String dateTimeFormatterToIsoLocalDate, long minusDay) {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.minusDays(minusDay);
        LocalDate parse = LocalDate.parse(dateTimeFormatterToIsoLocalDate, DateTimeFormatter.ISO_LOCAL_DATE);
        return parse.isBefore(localDate);
    }

    /**
     * 验证时间是否过期（年）
     * @param dateTimeFormatterToIsoLocalDate 需要比较的时间，日期格式：'yyyy-MM-dd'
     * @param minusYear 超过的年数
     * @return true 过期，false 未过期
     */
    public static boolean isExpireOnYear(String dateTimeFormatterToIsoLocalDate, long minusYear) {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.minusYears(minusYear);
        LocalDate parse = LocalDate.parse(dateTimeFormatterToIsoLocalDate, DateTimeFormatter.ISO_LOCAL_DATE);
        return parse.isBefore(localDate);
    }


    /**
     * 时间戳转日期格式（秒级）
     * @param timeStamp 时间戳
     * @return 对应日期格式：'yyyy-MM-dd'
     */
    public static String timeStampToLocalDateTime(long timeStamp) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeStamp, 0, ZoneOffset.ofHours(8));
        return localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
    }

    /**
     * 日期转时间戳（秒级）
     *
     * 日期格式为：'yyyy-MM-dd'
     *
     * @param time 日期
     * @return 时间戳（秒级）
     */
    public static long localDateTimeToTimeStamp(String time) {
        LocalDate parse = LocalDate.parse(time, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDateTime localDateTime = parse.atStartOfDay();
        return localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
    }
}
